package com.sw.study.scn.controller;

import com.sw.study.scn.pojo.Token;
import com.sw.study.scn.service.CodeServiceFeignClient;
import com.sw.study.scn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    private Integer port;

    //
    @GetMapping("/register/{email}/{password}/{code}")
    public Map<String,String> registerUser(@PathVariable("email") String email,
                @PathVariable("password") String password,
                @PathVariable("code") String code, HttpServletResponse response) {
        Map<String,String> resultMap = new HashMap<>();
       int validateResult = codeServiceFeignClient.validateEmail(email,code);
        if(validateResult==1){
            resultMap.put("message","数据不存在");
            resultMap.put("success","false");
        }else if(validateResult==2){
            resultMap.put("message","验证码超时");
            resultMap.put("success","false");
        }else{
            resultMap.put("message","成功");
            resultMap.put("success","true");
            userService.saveToken(email,password);
          Cookie cookie = new Cookie("token",email+"_"+password);
          cookie.setPath("/");
            response.addCookie(cookie);
            return resultMap;
        }

        return resultMap;
    }


  @GetMapping("/login/{email}/{password}")
  public Map<String,String> login(@PathVariable("email") String email,
      @PathVariable("password") String password,
      HttpServletResponse response) {
    Map<String,String> resultMap = new HashMap<>();
    String token = email+"_"+password;
    Token Token = userService.findOne(token);
    if(Token==null){
      resultMap.put("message","邮箱或密码错误");
      resultMap.put("success","false");
    }else{
      Cookie cookie = new Cookie("token",email+"_"+password);
      cookie.setPath("/");
      response.addCookie(cookie);
      resultMap.put("message","成功");
      resultMap.put("success","true");
    }

    return resultMap;
  }


  @GetMapping("/info/{token}")
  public Map<String,String> userInfo(@PathVariable("token") String token) {
    Map<String,String> resultMap = new HashMap<>();
    Token Token = userService.findOne(token);
    if(Token==null){
      resultMap.put("message","token错误");
      resultMap.put("success","false");
    }else{
      resultMap.put("message",Token.getEmail());
      resultMap.put("success","true");
    }

    return resultMap;
  }
}
